/**
 * Created by gongxun on 16/11/19.
 */

var KwxPlayerModel = function(){
    var that = {};

    that.seat = null;

    that.inhandCards = [];

    that.dealCards = [];

    that.openCards = [];

    that.isFromCard = false;

    //清空
    that.reset = function(){
        that.inhandCards = [];
        that.dealCards = [];
        that.openCards = [];
        that.isFromCard = false;
    };

    //发牌动画
    that.setInhandCards = function(cards){
        that.isFromCard = (cards.length == 14);
        that.inhandCards = cards;
        mo.kwxCardHelper.sortCardForNum(that.inhandCards);

        for (var i = 0; i < that.inhandCards.length; i++) {
            var card = that.inhandCards[i];
            card.setCardKind(that.seat, 1);
            var p = mo.kwxCardHelper.getInhandCardPos(that.seat, i, that.openCards.length, (that.isFromCard && (i == that.inhandCards.length - 1)));
            card.setPosition(p);
            card.setOpacity(0);
            card.runAction(cc.sequence(
                cc.delayTime(0.02 * i),
                cc.callFunc(function(){
                    this.setOpacity(255);
                }, card)
            ));
        }
    };

    //添加手牌
    that.addInhandCard = function(card){
        that.isFromCard = true;
        that.inhandCards.push(card);

        card.setCardKind(that.seat, 1);
        var index = that.inhandCards.length - 1;
        var p = mo.kwxCardHelper.getInhandCardPos(that.seat, index, that.openCards.length, (that.isFromCard && (index == that.inhandCards.length - 1)));
        card.setPosition(p);
    };

    //打出手牌
    that.setDealCard = function(cardNum){
        that.isFromCard = false;
        var card = null;

        //先从站着的牌里面找
        if (that.selectInhandCardIndex){
            if (that.selectInhandCardIndex < that.inhandCards.length){
                var inhand = that.selectInhandCardIndex;
                if (that.inhandCards[inhand].mCardNum == cardNum){
                    card = that.inhandCards[inhand];
                    that.inhandCards.splice(inhand, 1);
                }
            }
        }

        //站着的牌没找到 从手牌里面找
        if (card == null){
            //倒着倒
            for (var i = that.inhandCards.length - 1; i >= 0; i--){
                if (that.inhandCards[i].mCardNum == cardNum){
                    card = that.inhandCards[i];
                    that.inhandCards.splice(i, 1);
                    break;
                }
            }
        }

        //手牌里面没找到 就拿末尾的
        if(card == null){
            var index = that.inhandCards.length - 1;
            card = that.inhandCards[index];
            that.inhandCards.splice(index, 1);
        }

        if (card){
            //找到了
            card.setCardNum(cardNum);
            card.setCardKind(that.seat, 2);
            that.dealCards.push(card);
            var p = mo.kwxCardHelper.getDealCardPos(that.seat, that.dealCards.length - 1);
            card.stopAllActions();
            card.setPosition(p);
            card.setOpacity(0);
            var fadeTime = 0.2;
            card.runAction(cc.sequence(
                cc.fadeIn(fadeTime)
            ));
            card.mNumSprite.runAction(cc.sequence(
                cc.fadeIn(fadeTime)
            ));
            card.mFlagSprite.runAction(cc.sequence(
                cc.fadeIn(fadeTime)
            ));
        }

        that.setDealCardAction();
    };

    //添加开口牌
    that.setOpenCard = function(){
        that.isFromCard = false;
        for (var i = 0; i < that.openCards.length; i++){
            var cards = that.openCards[i].cards;
            var type = that.openCards[i].type;
            for (var j = 0; j < cards.length; j++){
                var card = cards[j];
                var num = card.mCardNum;

                if (type == 5){
                    if (j != 3){
                        card.setCardKind(that.seat, 4);
                    }else{
                        card.setCardKind(that.seat, num == 0 ? 4 : 3);
                    }
                }else{
                    card.setCardKind(that.seat, 3);
                }

                var p = mo.kwxCardHelper.getOpenCardPos(that.seat, i, j);
                card.stopAllActions();
                card.setOpacity(255);
                card.setPosition(p);

                if (j == 3){
                    card.mCardZOrder = mo.gameSize.y;
                }
            }
        }

        that.sortInahndCard();
    };

    //玩家手牌中的亮的牌
    that.setLiangCard = function(liangCardNums){
        if (liangCardNums.length == 0){
            return;
        }

        //检查手牌中是否全部都有了
        var isExist = true;
        for(var i = 0; i < liangCardNums.length; i++){
            var num = liangCardNums[i];
            var cunzai = false;
            for (var j = 0; j < that.inhandCards.length; j++) {
                if (that.inhandCards[j].mCardNum == num){
                    cunzai = true;
                    break;
                }
            }
            if (!cunzai){
                isExist = false;
                break;
            }
        }

        if (isExist){
            //存在就不管啦
        }else {
            //不存在就把开头的牌换成这个
            for(var i = 0; i < liangCardNums.length; i++){
                that.inhandCards[i].setCardNum(liangCardNums[i]);
                that.inhandCards[i].setCardKind(that.seat, 1);
            }
        }

        //不搞这个
        // //亮牌后把手牌放到除了 最后起的牌
        // for (var j = 0; j < that.inhandCards.length; j++) {
        //     var IsPlayCardAndLast = that.isFromCard && (j == that.inhandCards.length - 1);
        //     if (that.inhandCards[j].mCardNum == 0 && !IsPlayCardAndLast){
        //         that.inhandCards[j].setCardKind(that.seat, 4);
        //     }
        // }

    };

    //自己的是上标记 其他玩家是放到所有没亮的手牌
    that.setKouCard = function(kouCardNums){
        if (kouCardNums.length == 0){
            return;
        }

        if (that.seat == 0){
            var backIndex = [];
            var cardNums = [];
            for (var i = 0; i < that.inhandCards.length; i++) {
                cardNums.push(that.inhandCards[i].mCardNum);
            }
            for(var i = 0; i < kouCardNums.length; i++){
                var findIndex = mo.gameHelper.findNumApartIndexs(cardNums, kouCardNums[i], backIndex);
                if (findIndex != null){
                    backIndex.push(findIndex);
                }
            }
            for(var i = 0; i < backIndex.length; i++){
                that.inhandCards[backIndex[i]].setFlag(3);
            }

        }else {
            for (var i = 0; i < that.inhandCards.length; i++) {
                if (that.inhandCards[i].mCardNum == 0) {
                    that.inhandCards[i].setCardNum(0);
                    that.inhandCards[i].setCardKind(that.seat, 4);
                    that.inhandCards[i].setScale(mo.kwxCardHelper.leftInhandShowCardScale);
                    that.inhandCards[i].setAnchorPoint(cc.p(0.5, 0.238));

                    if (mo.gameMsgManager.kwxGameModel == 2){
                        that.inhandCards[i].setCardKind(that.seat, 4);
                        that.inhandCards[i].setScale(mo.kwxCardHelper.oppoInhandCardScale);
                        that.inhandCards[i].setAnchorPoint(cc.p(0.5, 0));
                    }
                }
            }
        }
    };

    //如果自己亮牌了 就显示其他玩家的手牌
    that.setLiangedShowHandCard = function(handCardNums){
        if (handCardNums.length == 0){
            return;
        }

        for (var i = 0; i < that.inhandCards.length; i++) {
            var card = that.inhandCards[i];
            var cardNum = handCardNums[i];

            card.setCardNum(cardNum);
            card.setCardKind(that.seat, 1);
            var p = mo.kwxCardHelper.getInhandCardPos(that.seat, i, that.openCards.length, (that.isFromCard && (i == that.inhandCards.length - 1)));
            card.setPosition(p);
        }
    };

    //飞牌排序
    that.setDealCardAction = function(){
        that.selectInhandCardIndex = null;
        if (that.inhandCards.length == 0){
            return;
        }

        if (that.seat == 0){
            if (mo.gameHelper.projectCode == 0){
                //如果0就不飞牌
                that.sortInahndCard();
                return;
            }

            //飞牌动画
            var flyCard = null;
            var tempCards = [];
            for (var i = 0; i < that.inhandCards.length; i++){
                tempCards.push(that.inhandCards[i]);
            }
            mo.kwxCardHelper.sortCardForNum(that.inhandCards);
            if (tempCards[tempCards.length - 1] != that.inhandCards[that.inhandCards.length - 1]){
                flyCard = tempCards[tempCards.length - 1];
            }

            var time = 0.3;
            var time1 = time / 2;
            for (var i = 0; i < that.inhandCards.length; i++){
                var card = that.inhandCards[i];
                card.stopAllActions();
                card.setOpacity(255);
                var endPos = mo.kwxCardHelper.getInhandCardPos(that.seat, i, that.openCards.length, false);
                if (card == flyCard){
                    var rotate1 = 30;
                    var rotate2 = 0;
                    var cardHeight = mo.kwxCardHelper.getInhnadCardSize().height;
                    // card.runAction(cc.sequence(
                    //     cc.spawn(cc.rotateTo(time, rotate1), cc.moveBy(time, cc.p(0, cardHeight + 10))),
                    //     cc.moveTo(time, cc.p(endPos.x, cardHeight + 10)),
                    //     cc.spawn(cc.rotateTo(time, rotate2), cc.moveTo(time, endPos))
                    // ));
                    card.runAction(cc.sequence(
                        cc.moveBy(time, cc.p(0, cardHeight + 20)),
                        cc.moveTo(time, cc.p(endPos.x, cardHeight + 20)),
                        cc.moveTo(time, endPos)
                    ));
                }else{
                    card.setRotation(0);
                    card.runAction(cc.sequence(cc.moveTo(time * 2, endPos)));
                }
            }

        }else{
            that.sortInahndCard();
        }
    };

    //强制排序
    that.sortInahndCard = function(){
        that.isFromCard = false;
        that.selectInhandCardIndex = null;
        if (that.inhandCards.length == 0){
            return;
        }

        mo.kwxCardHelper.sortCardForNum(that.inhandCards);
        for (var i = 0; i < that.inhandCards.length; i++){
            var card = that.inhandCards[i];
            card.stopAllActions();
            card.setOpacity(255);

            var p = mo.kwxCardHelper.getInhandCardPos(that.seat, i, that.openCards.length, false);
            card.setRotation(0);
            card.setPosition(p);
        }
    };

    that.resetPlayer = function(){
        //inhand
        mo.kwxCardHelper.sortCardForNum(that.inhandCards);
        for (var i = 0; i < that.inhandCards.length; i++){
            var card = that.inhandCards[i];
            card.setCardKind(that.seat, 1);
            var p = mo.kwxCardHelper.getInhandCardPos(that.seat, i, that.openCards.length, (that.isFromCard && (i == that.inhandCards.length - 1)));
            card.setPosition(p);
        }

        //deal
        for (var i = 0; i < that.dealCards.length; i++){
            var card = that.dealCards[i];
            card.setCardKind(that.seat, 2);
            var p = mo.kwxCardHelper.getDealCardPos(that.seat, i);
            card.setPosition(p);
        }

        //open
        that.setOpenCard();
    };

    that.init = function(){
        that.reset();
    };

    that.init();

    return that;
};